package com.fast.security.handler;

import com.fast.enums.CodeEnum;
import com.fast.pojo.AjaxResult;
import com.fast.utils.response.ResponseUtil;
import org.springframework.http.MediaType;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author ruan cai yuan
 * @version 1.0
 * @fileName com.fast.security.handler.FailureHandler
 * @description: 登录失败的拦截器
 * @since 2024/6/23 14:48
 */
@Component
public class AuthenticationFailureHandlerImpl implements AuthenticationFailureHandler {
    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
        AjaxResult AuthenticationFailed = AjaxResult.error(CodeEnum.AuthenticationFailed.getCode(), CodeEnum.AuthenticationFailed.getMsg());
        ResponseUtil.responseData(response, AuthenticationFailed, MediaType.APPLICATION_JSON_VALUE);
    }
}
